var dxpz = {
    setBtnStatus: function () {
        var row = $('#grid_dxpz').datagrid('getSelected');
        if (row && row.id &&row.sfqy == '否') {
            $("#btn_dxpz_mod").show();
            $("#btn_dxpz_start").show();
        } else if(row && row.id &&row.sfqy == '是'){
            $("#btn_dxpz_mod").hide();
            $("#btn_dxpz_start").show();
        }else {
            $("#btn_dxpz_mod").hide();
            $("#btn_dxpz_start").hide();
        }
    },
    alwayShowDetail:function (rowData) {
        $('#layout_dxpz').layout('remove', 'center');
        $('#layout_dxpz').layout('add', {
            region:'center',
            split:true,
            href:$.webPath() + '/base/dxpz/dxpz_detail?dxpzId=' + rowData.id
        });
    },
    showDetail:function (rowData) {
        if ($("#dxpz_detail_id").val() != rowData.id) {
            dxpz.alwayShowDetail(rowData);
        }
    }
}

$(function () {
    //表格初始加载
    $('#grid_dxpz').datagrid({
        url: $.webPath() + "/base/dxpz/dxpzList",
        toolbar: "#toolbar_dxpz",
        fitColumns: true,
        pageNumber: 1,
        singleSelect: true,
        pagination: true,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'mc', title: '名称', width: 30},
                {field: 'dxbm', title: '编码', width: 35},
                {field: 'fsrList', title: '发送人', width: 40},
                {field: 'sfqy', title: '启用', width: 8,
                    formatter:function (value) {
                        if (value == '是')
                            return '是';
                        else
                            return '否';
                    }
                },
                {field: 'dxmb', title: '模板', width: 150}
            ]
        ],
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
            dxpz.setBtnStatus();
        },
        onClickRow: function () {
            dxpz.setBtnStatus();
        },
        onDblClickCell:function (index, field) {
            if (field == "mc") {
                var row = $(this).datagrid("getSelected");
                if (row.sfqy == null || row.sfqy == '否')
                dxpz_modify('修改短信配置', 'update_dxpz');
            }
        },
        onSelect:function (rowIndex, rowData) {
            dxpz.showDetail(rowData);
            if (rowData.sfqy == null || rowData.sfqy == '否') {
                $("#btn_dxpz_start").text('启用');
                $("#btn_dxpz_start").removeClass('stop').addClass('start');
                $('#btn_dxpz_start').unbind('click');
                $('#btn_dxpz_start').bind('click', function () {
                    changeDxpzState($.webPath() + '/base/dxpz/start_dxpz', '确认启用此短信配置？');
                });
            } else {
                $("#btn_dxpz_start").text('停用');
                $("#btn_dxpz_start").removeClass('start').addClass('stop');
                $('#btn_dxpz_start').unbind('click');
                $('#btn_dxpz_start').bind('click', function () {
                    changeDxpzState($.webPath() + '/base/dxpz/stop_dxpz', '确认停用此短信配置？');
                });
            }
            dxpz.setBtnStatus();
        },
        rowStyler:function(index,row){
            if(row.sfqy == '否') {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        }
    });

    //**********按钮触发事件**********
    //修改按钮
    $("#btn_dxpz_mod").click(function () {
        dxpz_modify('修改短信配置', 'update_dxpz');
    });
    //启用-停用按钮
    $("#btn_dxpz_start").click(function () {
        changeDxpzState();
    });
    //查询按钮
    $("#btn_dxpz_search").click(function () {
        dxpz_search();
    });
});

//修改短信配置
function dxpz_modify(title, action) {
    var record = $("#grid_dxpz").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录！", "warning");
        return;
    }
    var href = $.webPath() + "/base/dxpz/dxpz_content?dxpzId=" + record.id;
    var actionUrl = $.webPath() + "/base/dxpz/" + action;
    dxpz_showDialog(title, href, actionUrl);
}

//查询短信配置
function dxpz_search() {
    $('#grid_dxpz').datagrid('load',
        {
            mc:$('#dxpz_mc').val()
        }
    );
}

//新增或修改短信配置弹出窗口
function dxpz_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:820,
        height:400,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $(win).mask();
                    $('#form_dxpz').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_dxpz').datagrid('reload');
                                $('#layout_dxpz').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//改变状态，启用停用
function changeDxpzState(url, message) {
    var dxpzId = $("#grid_dxpz").datagrid("getSelected").id;
    if (dxpzId) {
        $.messager.confirm("确认", message, function (r) {
            if (r) {
                $.ajax({
                    url:url + "?dxpzId=" + dxpzId,
                    type:"json",
                    success:function (data) {
                        if (data.statusCode == '200') {
                            $('#grid_dxpz').datagrid('reload');
                            $.messager.show({
                                title:'提示框',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $.messager.alert('提示', data.message, 'info');
                        }
                    }
                });
            }
        })
    }
}